package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Department;
import oa.entity.User;
import oa.exception.DaoException;
import oa.service.UserService;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class DeptupServlet
 */
@WebServlet("/deptupServlet")
public class DeptupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeptupServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	    PrintWriter out = response.getWriter();
	    UserService userim = new UserServiceImpl();
	    DepartmentServiceImpl deptimpl = new DepartmentServiceImpl();
	    User us = new User();
	    Department department = new Department();
	    String name = request.getParameter("val-name");
		String pon = request.getParameter("val-phone");
		String deptPrincipal = request.getParameter("val-deptPrincipal");
		String bu = request.getParameter("val-deptBlurb");
		String id = request.getParameter("dep");
		String compantid = request.getParameter("compantid");
		boolean yanzheng = false;
	    boolean falg =false;
		List<User> useridlist = new ArrayList<User>();
		int Competence = 0;
		
		try {
			/*
			 * 初始化加载到本部门原有的负责人
			 */
			
			useridlist=userim.selectUser(Integer.parseInt(deptPrincipal),null,0,0);
			for (User items : useridlist) {
				Competence =items.getUserCompetence();
			}
			if(Competence==1) {
				// 根据员工的id更新它的权限
				us.setUserPosition("员工");
				us.setUserCompetence(0);
				us.setUserId(Integer.parseInt(deptPrincipal));
				yanzheng = userim.updateUserFuZe(us);
			}else if(Competence==0) {
				// 根据员工的id更新它的权限
				us.setUserPosition("经理");
				us.setUserCompetence(1);
				us.setUserId(Integer.parseInt(deptPrincipal));
				yanzheng = userim.updateUserFuZe(us);
			}
			if(name!=null&&pon!=null&&deptPrincipal!=null&&bu!=null) {
				department.setDeptName(name);
				department.setDeptPhone(pon);
				department.setDeptPrincipal(Integer.parseInt(deptPrincipal));
				department.setDeptBlurb(bu);
				department.setDeptId(Integer.parseInt(id));
				department.setCompanyId(Integer.parseInt(compantid));
			    falg = deptimpl.updateDepartment(department);
			}
			out.print(JSON.toJSONString(falg));

		} catch (DaoException e) {
			
			e.printStackTrace();
		}

		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
